********************************
***Fix Message Vary Source***
********************************

clear all

********************
*Set Person Working*
*Change this to your name!!!!!
********************
*global carolina 0
*global emily 1

global emily 1
global carolina 0

*********************
*Load Files*
*********************

*Carolina's Computer*
if $carolina ==0  {
cap cd "E:\Dropbox\Carolina-Emily-Project\Data\Study"
global out "..\..\Results\"
}


if $emily ==1  {
*Emily's Computer*
cap cd "~/Dropbox/SourceContent/Data/Study"
global out "../../Results/"
} 

use 3_clean_data, replace


/*Sample KSmirnov Code*
gen groupk=1 if treatment==1
replace groupk=0 if  treatment==0
ksmirnov ln_prob_level_anti  if democrat==0 & (treatment==1 | treatment==0), by(group)

r(p_1) is probability that the first group distribution is less than the second group
r(p_2) is probability that the first group distribution is greater than the second group
r(p) gives combined probability of difference
*/

*********************************
***Create Variables and Format***
*********************************

global lasso0 hispanic age55_64 candidate16_hillary candidate16_other ///
	immlevel_decrease gun_lessstrict abortion_illegal tax_toohigh health_notgvt ///
	occ_twitter occ_buzzfeed trumpfan lebronfan taylorfan bgatesfan ///
	obamafan obama_neutral topissue_health 
	
global lasso1 black hispanic age35_44 age45_54 hsdegree ///
	candidate16_hillary immlevel_decrease immlevel_same ///
	gun_same gun_lessstrict abortion_partlegal abortion_illegal tax_toohigh ///
	health_neutral health_notgvt i.freq_nytimes ///
	daily_tv occ_tv daily_newspaper i.freq_fox week_breitbart ///
	daily_breitbart occ_buzzfeed ///
	lebron_neutral obamafan trump_neutral trumpfan west topissue_tax 

	
/*Order or Treatment Groups*
5 7 6 8 1 3 2 4 9 10
*/

gen group1=treatment==5 | treatment==7 | treatment==0
gen group2=treatment==6 | treatment==8 | treatment==0
gen group3=treatment==1 | treatment==3 | treatment==0
gen group4=treatment==2 | treatment==4 | treatment==0

global message1 "Anti"
global message2 "Anti"
global message3 "Pro"
global message4 "Pro"

global source1 "Trump"
global source2 "Obama"
global source3 "Trump"
global source4 "Obama" 


foreach y in 1 3 {
	global space`y' "[0.1em]" 
	} 
foreach y in 2 {
	global space`y' "[1em]" 
	} 
foreach y in 4 {
	global space`y' "[0.65em]" 
	} 

gen president=treatment==1 | treatment==2 | treatment==5 | treatment==6
gen actor=treatment==3 | treatment==4 | treatment==7 | treatment==8
gen message=actor==1 | president==1

global color1 "blue"
global color0 "red"

*gen fan_president=obamafan if treatment==6 | treatment==8 | treatment==2 | treatment==4
*replace fan_president=trumpfan if treatment==5 | treatment==7 | treatment==1 | treatment==3


*****************************
*****************************
*****************************
global party0 "Republicans"
global party1 "Democrats"
global var0 "ln(Probability Anti-Immigrant + 1)"
global var1 "ln(Probability Pro-Immigrant + 1)"
global rule0 "\toprule"
global rule1 "\midrule"
global dir0 "anti"
global dir1 "pro"
global Dir0 "Anti"
global Dir1 "Pro"

****************************************
*TABLES - Pro/Anti VERSION*
****************************************

	
file close _all
capture macro drop fh
file open fh using "${out}3_TotalEffect.tex", write replace
file write fh ///
"\hspace*{-1cm}\small{\begin{tabular}{llccccc}"  _n 

foreach p in 0 1 {
	
sum prob_index_${dir`p'} if treat==0 & recruit==`p'
local y`p'=round(`r(mean)'*1000)/1000
local y`p': di %6.3f `y`p''

sum ln1_prob_index_${dir`p'} if treat==0 & recruit==`p'
local ly`p'=round(`r(mean)'*1000)/1000
local ly`p': di %6.3f `ly`p''

file write fh ///
"${rule`p'}" _n ///
" & & \multicolumn{5}{c}{\textbf{${party`p'}}}   \\ [0.75em] " _n ///
" & & \multicolumn{5}{c}{\textit{${var`p'}}}   \\" _n ///
" \textbf{Message} & \textbf{Source} & $\beta_t$ &  (S.E.) & \% Diff. & \textit{P(Dist.)} &  N \\" _n ///
"\midrule \\" _n	

foreach y in 1 2 3 4 {
 
	********************
	*Total Regression*
	********************
	
	reg ln1_prob_index_${dir`p'} president ///
		${lasso`p'} if (group`y'==1 & actor==0) & recruit==`p', robust
		
	count if e(sample)
	local nt`p'=r(N)

	*Beta T - President*	
	local bt`p' = round(_b[president]*1000)/1000
	local bt`p' : di %6.3f `bt`p''
	local st`p' = round(_se[president]*1000)/1000
	local st`p' : di  %6.3f `st`p''
	local p`p' = 2*ttail(e(df_r),abs(_b[president]/_se[president]))
	if `p`p''<=0.1 & `p`p''>0.05 {
		global start`p' "*"
		}
	if `p`p''<=0.05 & `p`p''>0.01 {
		global start`p' "**"
		}
	if `p`p''<=0.01 {
		global start`p' "***"
		}
	else if `p`p''>0.1 {
		global start`p' ""
		}
	sum prob_index_${dir`p'} if treatment==0 & e(sample)==1
	local m=r(mean)
	local ct`p'=round(_b[president]*(`m'+1)/`m'*10000)/100
	local ct`p' : di  %6.2f `ct`p''
	
	*Distribution Difference - President vs. Control*
	reg ln1_prob_index_${dir`p'} ${lasso`p'} if (group`y'==1 & actor==0) & recruit==`p'
	predict res, res
	gen g=0 if president==0 & e(sample)==1
	replace g=1 if president==1 & e(sample)==1
	ksmirnov res if e(sample), by(g)
	local dt`p'=round(r(p)*1000)/1000
	local dt`p': di %6.3f `dt`p''
	drop g res
	
	file write fh ///
	"${message`y'} & ${source`y'} & `bt`p''${start`p'} & (`st`p'') & `ct`p''\% & \textit{`dt`p''} & `nt`p'' 	\\" _n ///
	"${space`y'}" _n	
	
}

file write fh ///
" \multicolumn{7}{r}{Control Group Mean: \textit{ln(P(${Dir`p'})+1)}=`ly`p'', \textit{P(${Dir`p'})}=`y`p''}   \\ \\" _n ///
 

}

file write fh ///
"\bottomrule" _n ///
"\end{tabular}}\hspace*{-1cm}" _n
file close fh







